package bb.lanxing.lib.devices.sprint.model.impl;

import android.content.Context;
import android.net.Uri;
import androidx.exifinterface.media.ExifInterface;
import bb.lanxing.lib.devices.sprint.entity.SprintFirmware;
import bb.lanxing.lib.devices.sprint.model.SprintFirmwareModel;
import java.io.IOException;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public abstract class AbstractSprintFirmwareModel implements SprintFirmwareModel {
    private Subscription laodFirmwareSub;
    private String versionDesc;
    private String versionName;
    private String versionUri;

    protected abstract List<SprintFirmware> loadFirmwares() throws IOException;

    @Override
    public void requestFirmwareVersion(Context context) {
        if (context == null) {
            return;
        }

        this.laodFirmwareSub = Observable.create((Observable.OnSubscribe<String>) subscriber -> {
            try {
                List<SprintFirmware> loadFirmwares = AbstractSprintFirmwareModel.this.loadFirmwares();
                if (subscriber.isUnsubscribed()) {
                    return;
                }
                if (loadFirmwares != null) {
                    String str = "";
                    String str2 = str;
                    int i = 0;
                    int i2 = 0;
                    for (SprintFirmware sprintFirmware : loadFirmwares) {
                        if (sprintFirmware != null) {
                            int mainVersion = sprintFirmware.getMainVersion();
                            int subVersion = sprintFirmware.getSubVersion();
                            String firmwareUrl = sprintFirmware.getFirmwareUrl();
                            String description = sprintFirmware.getDescription();
                            if (mainVersion > i || (mainVersion >= i && subVersion >= i2)) {
                                str = description;
                                i = mainVersion;
                                i2 = subVersion;
                                str2 = firmwareUrl;
                            }
                        }
                    }
                    String str3 = ExifInterface.GPS_MEASUREMENT_INTERRUPTED + i + "." + i2;
                    AbstractSprintFirmwareModel.this.onNewestFirmwareVersion(str3, str, str2);
                    subscriber.onNext(str3);
                }
                subscriber.onCompleted();
            } catch (IOException e) {
                subscriber.onError(e);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onNext(String str) {
            }
        });
    }

    public void onNewestFirmwareVersion(String str, String str2, String str3) {
        this.versionDesc = str2;
        this.versionName = str;
        this.versionUri = str3;
    }

    public void setVersionName(String str) {
        this.versionName = str;
    }

    public void setVersionDesc(String str) {
        this.versionDesc = str;
    }

    public void setVersionUri(String str) {
        this.versionUri = str;
    }

    @Override
    public String getFirmwareVersionName() {
        return this.versionName;
    }

    @Override
    public String getFirmwareVersionDesc() {
        return this.versionDesc;
    }

    @Override
    public Uri getFirmwareUri() {
        String str = this.versionUri;
        if (str != null) {
            return Uri.parse(str);
        }
        return null;
    }

    @Override
    public void release() {
        Subscription subscription = this.laodFirmwareSub;
        if (subscription != null) {
            subscription.unsubscribe();
            this.laodFirmwareSub = null;
        }
    }
}
